%  =======================================================================
%  Matlab Data Import and Real Time Plotting
%
%  By Patrick Yukman
%  Dartmouth College
%  =======================================================================

%%
%  The port name will be system dependent. On different systems it might
%  look like:
%
%  Linux: port = '/dev/ttyS0';
%  MacOS: port = '/dev/tty.usbmodemft121';
%  Windows: port = 'COM1';
%
%  But it will need to be correctly configured before you can receive any 
%  data!
clear all;
port = '/dev/tty.usbmodemfd121';

%%
%  Variables
X_SIZE = 4000;

%%
%  This code serves to open the serial port for communication between the
%  Arduino and the computer.
in = serial(port);
set(in, 'BaudRate', 115200);
set(in, 'Parity', 'none');
set(in, 'DataBits', 8);
set(in, 'StopBits', 1);
set(in, 'InputBufferSize', 1000);
fopen(in);
tline = fgets(in);
%%
%  This code will open and set up an external file to which the data will
%  be separately written.
date = datestr(now);
dateString = strrep(date, ' ', '_');
dateString = strrep(dateString, ':', '');
dataTitle = ['DataInput_' dateString '.txt'];
dataFile = fopen(dataTitle,'w');
disp(['Data being written to ' dataTitle]);

%%
%  Now we will set up the figure window.
%voltages1 = zeros(1,X_SIZE);
%voltages2 = zeros(1,X_SIZE);
%xSpacing = linspace(1,X_SIZE,X_SIZE);
%dataLine1 = plot(xSpacing,voltages1,'-s','MarkerSize',2);%for plot sweep only
%dataLine1 = plot(voltages1,voltages2,'-s','MarkerSize',2);%for plot I-Vcrv
%ylim([0 1023]);
%xlim([0 512]);

%%
%  Now we read data in through the serial port.
tline = 1;
count = 1;
%  Put everything into a try/catch block to exit gracefully 
%try
   while (tline ~= -1)
       %  Read a line from the serial port, write it to a file, and plug
       %  it into the voltages vector.
       tline = fgets(in);
       fprintf(dataFile,tline);

       %k = strfind(tline, ',');
       %L=length(tline);
       %num1=str2num(tline(1:k-1));
       %num2=str2num(tline(k+1:L)); 

       %num = str2num(tline);
       %voltages1(count) = num1;
       %voltages2(count) = num2;
       %  Increment count all the way to X_SIZE, then start at the
       %  beginning again. This lets us keep writing over the elements of
       %  voltages, so the script doesn't get bogged down with memory 
       %  allocation and massive vector structures.
       %if count <= (X_SIZE - 1)
       %    count = count + 1;
       %else
       %    count = 1;
       %end
       %  Only redraw once every 25 data points. This allows the plotting
       %  to happen relatively quickly. Plotting each data point from the
       %  Arduino, which is running at ~1000 Hz, would slow the script
       %  down so much that the Arduino's data buffer would fill up.
       %if mod(count,2000) == 0
       %    set(dataLine1,'XData',voltages1);%this for I-V curve
       %    set(dataLine1,'YData',voltages2);%also for I-V curve

           %set(dataLine1,'YData',voltages1);%choice for plot of sweep only


       %    drawnow;

       end
%    end
%catch err
   fclose(in);
   disp('Serial port successfully closed.');
%end